#include "json.h"
#include "AbstractFactory.h"
#include "ClassObjectPool.h"
#include "UIBase.h"

#include <iostream>
#include <fstream>
#include <string>
extern "C" void InitGUI();

#ifdef GUIEXE
int main() {
    InitGUI();
    //1. 读取配置文件转为json对象
    Json::Value GlobalCustomeCfg = Json::nullValue;
    std::ifstream ifs;
    ifs.open("../Test/jsonFile.json", std::ios::in);
    if(!ifs.is_open()) {
        return -1;
    }
    std::string content;
    while(!ifs.eof()) {
        std::string str;
        ifs >> str;
        content += str;
    }
    ifs.close();
    Json::Reader jsonReader;
    jsonReader.parse(content, GlobalCustomeCfg);
    //2. 解析json对象: 1. 依据json生成对象，2.将生成的对象insert到对象池中
    Json::Value::Members mem = GlobalCustomeCfg.getMemberNames();
    for(auto iter = mem.begin(); iter != mem.end(); ++iter) {
        ClassObjectPool obj;
        obj.Insert( (*iter), AbstractFactory::getObject<UIBase, Json::Value&>(GlobalCustomeCfg[*iter]["property"].asString(), GlobalCustomeCfg[*iter]));
    }
    return 0;
}
#else
extern "C" void StartGUI(const std::string& fileName) {
    InitGUI();
    //1. 读取配置文件转为json对象
    Json::Value GlobalCustomeCfg = Json::nullValue;
    std::ifstream ifs;
    ifs.open(fileName, std::ios::in);
    if(!ifs.is_open()) {
        return ;
    }
    std::string content;
    while(!ifs.eof()) {
        std::string str;
        ifs >> str;
        content += str;
    }
    ifs.close();
    Json::Reader jsonReader;
    jsonReader.parse(content, GlobalCustomeCfg);
    //2. 解析json对象: 1. 依据json生成对象，2.将生成的对象insert到对象池中
    Json::Value::Members mem = GlobalCustomeCfg.getMemberNames();
    for(auto iter = mem.begin(); iter != mem.end(); ++iter) {
        ClassObjectPool obj;
        obj.Insert( (*iter), AbstractFactory::getObject<UIBase, Json::Value&>(GlobalCustomeCfg[*iter]["property"].asString(), GlobalCustomeCfg[*iter]));
    }

}
#endif